const { Duplex } = require("stream");

let arr = ["1", " 2", "3", "4", "6", "7", "69"];

class MyDuplex extends Duplex {
  constructor(source, options) {
    super(options);
    this.source = source;
  }

  _read() {
    let data = this.source.shift() || null;
    this.push(data);
  }

  _write(chunk, encoding, callback) {
    if (Buffer.isBuffer(chunk)) {
      chunk = chunk.toString();
    }
    process.stdout.write(chunk + "\n");
    process.nextTick(callback);
  }
}

const myDuplex = new MyDuplex(arr);

// 监听 "data" 事件，流动模式自动触发 _read()
myDuplex.on("data", (chunk) => {
  console.log(`读取: ${chunk.toString()}`);
});

// 手动写入数据
myDuplex.write("100");
myDuplex.write("200");
